package com.software.driver.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.taskform.dao.TaskForm;
import com.software.form.taskform.dao.TaskFormTransaction;

public class UpdateTaskServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action = request.getParameter("action");
		TaskFormTransaction tft = (TaskFormTransaction) CommonUtils.getBean("taskFormTransaction");
		if(action.equals("update")){
			JSONObject json = CommonUtils.readJSONStringFromRequestBody(request, response);
			try {
				String status = json.getString("status");
				int id = (Integer) request.getSession().getAttribute("current");
				String s = tft.updateStatus(id, status);
				if(s != null){
					String[] results = s.split("/");
					String str = "";
					for(int i = 0; i < results.length; i++)
						str += results[i]+"<br />";
					JSONObject o = new JSONObject();
					o.put("status", str);
					CommonUtils.outputJsonData(o.toString(), response);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(action.equals("over")){
			int id = (Integer) request.getSession().getAttribute("current");
			String status = tft.updateEndTime(id,CommonUtils.getNowTime());
			if(status != null){
				String[] results = status.split("/");
				String str = null;
				for(int i = 0; i < results.length; i++)
					str += results[i]+"<br />";
				JSONObject o = new JSONObject();
				try {
					o.put("status", str);
					CommonUtils.outputJsonData(o.toString(), response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}else if(action.equals("add")){
			JSONObject o = CommonUtils.readJSONStringFromRequestBody(request, response);
			try{
				String no = o.getString("no");
				String startTime = o.getString("start");
				String dueTime = o.getString("due");
				String name = o.getString("name");
				String comment = o.getString("comment");
				
				if(tft.addTask(no, startTime, dueTime, comment, name))
					CommonUtils.outputJsonData(1, response);
				else
					CommonUtils.outputJsonData(0, response);
			}catch(JSONException je){
				try {
					CommonUtils.outputJsonData(0, response);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
}
